package models

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v2"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type mysqlconfig struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Name     string `yaml:"name"`
}

var DB *gorm.DB
var err error

func init() {
	yamlFile, _ := os.ReadFile("./config/config.yml")
	var mysqlconfig mysqlconfig
	yaml.Unmarshal(yamlFile, &mysqlconfig)
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", mysqlconfig.Username, mysqlconfig.Password, mysqlconfig.Host, mysqlconfig.Port, mysqlconfig.Name)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
}
